<?php
/**
 * Class that operate on table 'movie'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2013-05-22 23:17
 */
class MovieMySqlDAO implements MovieDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return MovieMySql 
	 */
	public function load($id){
		$sql = 'SELECT * FROM movie WHERE PKMovie = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->getRow($sqlQuery);
	}
	public function loadpk($pKMovie){
		$sql = 'SELECT * FROM movie WHERE PKMovie = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($pKMovie);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM movie';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM movie ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param movie primary key
 	 */
	public function delete($PKMovie){
		$sql = 'DELETE FROM movie WHERE PKMovie = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($PKMovie);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param MovieMySql movie
 	 */
	public function insert($movie){
		$sql = 'INSERT INTO movie (FKGender, Name, Stakeholders, Director, PicturePath, VideoPath, Format, Year, Rate, Price, PriceRentals, Languages, Subtitles, Description, Runtime, Active, Visible) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->setNumber($movie->fKGender);
		$sqlQuery->set($movie->name);
		$sqlQuery->set($movie->stakeholders);
		$sqlQuery->set($movie->director);
		$sqlQuery->set($movie->picturePath);
		$sqlQuery->set($movie->videoPath);
		$sqlQuery->set($movie->format);
		$sqlQuery->set($movie->year);
		$sqlQuery->setNumber($movie->rate);
		$sqlQuery->set($movie->price);
		$sqlQuery->set($movie->priceRentals);
		$sqlQuery->set($movie->languages);
		$sqlQuery->set($movie->subtitles);
		$sqlQuery->set($movie->description);
		$sqlQuery->set($movie->runtime);
		$sqlQuery->setNumber($movie->active);
		$sqlQuery->setNumber($movie->visible);

		$id = $this->executeInsert($sqlQuery);	
		$movie->pKMovie = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param MovieMySql movie
 	 */
	public function update($movie){
		$sql = 'UPDATE movie SET FKGender = ?, Name = ?, Stakeholders = ?, Director = ?, PicturePath = ?, VideoPath = ?, Format = ?, Year = ?, Rate = ?, Price = ?, PriceRentals = ?, Languages = ?, Subtitles = ?, Description = ?, Runtime = ?, Active = ?, Visible = ? WHERE PKMovie = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->setNumber($movie->fKGender);
		$sqlQuery->set($movie->name);
		$sqlQuery->set($movie->stakeholders);
		$sqlQuery->set($movie->director);
		$sqlQuery->set($movie->picturePath);
		$sqlQuery->set($movie->videoPath);
		$sqlQuery->set($movie->format);
		$sqlQuery->set($movie->year);
		$sqlQuery->setNumber($movie->rate);
		$sqlQuery->set($movie->price);
		$sqlQuery->set($movie->priceRentals);
		$sqlQuery->set($movie->languages);
		$sqlQuery->set($movie->subtitles);
		$sqlQuery->set($movie->description);
		$sqlQuery->set($movie->runtime);
		$sqlQuery->setNumber($movie->active);
		$sqlQuery->setNumber($movie->visible);

		$sqlQuery->setNumber($movie->pKMovie);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM movie';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByFKGender($value){
		$sql = 'SELECT * FROM movie WHERE FKGender = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByName($value){
		$sql = 'SELECT * FROM movie WHERE Name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByStakeholders($value){
		$sql = 'SELECT * FROM movie WHERE Stakeholders = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByDirector($value){
		$sql = 'SELECT * FROM movie WHERE Director = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByPicturePath($value){
		$sql = 'SELECT * FROM movie WHERE PicturePath = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByVideoPath($value){
		$sql = 'SELECT * FROM movie WHERE VideoPath = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByFormat($value){
		$sql = 'SELECT * FROM movie WHERE Format = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByYear($value){
		$sql = 'SELECT * FROM movie WHERE Year = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByRate($value){
		$sql = 'SELECT * FROM movie WHERE Rate = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByPrice($value){
		$sql = 'SELECT * FROM movie WHERE Price = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByPriceRentals($value){
		$sql = 'SELECT * FROM movie WHERE PriceRentals = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByLanguages($value){
		$sql = 'SELECT * FROM movie WHERE Languages = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryBySubtitles($value){
		$sql = 'SELECT * FROM movie WHERE Subtitles = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByDescription($value){
		$sql = 'SELECT * FROM movie WHERE Description = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByRuntime($value){
		$sql = 'SELECT * FROM movie WHERE Runtime = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByActive($value){
		$sql = 'SELECT * FROM movie WHERE Active = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByVisible($value){
		$sql = 'SELECT * FROM movie WHERE Visible = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByFKGender($value){
		$sql = 'DELETE FROM movie WHERE FKGender = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByName($value){
		$sql = 'DELETE FROM movie WHERE Name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByStakeholders($value){
		$sql = 'DELETE FROM movie WHERE Stakeholders = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByDirector($value){
		$sql = 'DELETE FROM movie WHERE Director = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByPicturePath($value){
		$sql = 'DELETE FROM movie WHERE PicturePath = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByVideoPath($value){
		$sql = 'DELETE FROM movie WHERE VideoPath = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByFormat($value){
		$sql = 'DELETE FROM movie WHERE Format = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByYear($value){
		$sql = 'DELETE FROM movie WHERE Year = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByRate($value){
		$sql = 'DELETE FROM movie WHERE Rate = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByPrice($value){
		$sql = 'DELETE FROM movie WHERE Price = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByPriceRentals($value){
		$sql = 'DELETE FROM movie WHERE PriceRentals = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByLanguages($value){
		$sql = 'DELETE FROM movie WHERE Languages = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteBySubtitles($value){
		$sql = 'DELETE FROM movie WHERE Subtitles = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByDescription($value){
		$sql = 'DELETE FROM movie WHERE Description = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByRuntime($value){
		$sql = 'DELETE FROM movie WHERE Runtime = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByActive($value){
		$sql = 'DELETE FROM movie WHERE Active = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByVisible($value){
		$sql = 'DELETE FROM movie WHERE Visible = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return MovieMySql 
	 */
	protected function readRow($row){
		$movie = new Movie();
		
		$movie->pKMovie = $row['PKMovie'];
		$movie->fKGender = $row['FKGender'];
		$movie->name = $row['Name'];
		$movie->stakeholders = $row['Stakeholders'];
		$movie->director = $row['Director'];
		$movie->picturePath = $row['PicturePath'];
		$movie->videoPath = $row['VideoPath'];
		$movie->format = $row['Format'];
		$movie->year = $row['Year'];
		$movie->rate = $row['Rate'];
		$movie->price = $row['Price'];
		$movie->priceRentals = $row['PriceRentals'];
		$movie->languages = $row['Languages'];
		$movie->subtitles = $row['Subtitles'];
		$movie->description = $row['Description'];
		$movie->runtime = $row['Runtime'];
		$movie->active = $row['Active'];
		$movie->visible = $row['Visible'];

		return $movie;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return MovieMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>